package com.leadership.api.handler.impl;

import java.io.File;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import com.demo2do.core.entity.exception.BusinessException;
import com.leadership.api.handler.MailHandler;

/**
 * 
 * @author houtao
 * 
 */
@Component("mailHandler")
public class MailHandlerImpl implements MailHandler {
    
    private static final Logger logger = LoggerFactory.getLogger(MailHandlerImpl.class);
    
    @Value("#{mail['mail.from']}")
    private String from;
    
    @Autowired
    private JavaMailSender javaMailSender;

	/* (non-Javadoc)
	 * @see com.ihg.hiex.scheduler.handler.MailHandler#sendEmail(java.lang.String, java.lang.String, java.lang.String)
	 */
	public void sendEmail(String email, String subject, String text) {
		
		try {
		    
		    logger.trace("#sendEmail() - form=[{}], email=[{}], subject=[{}]", from, email, subject);
			
			String [] to = new String [] {email};
			
			this.send(to, from, subject, text);
			
		} catch (Exception e) {
			logger.error("##sendEmail() - An error occurs when sending email.", e);
		}
		
	}
	
	/* (non-Javadoc)
	 * @see com.ihg.hiex.scheduler.handler.MailHandler#sendEmail(java.lang.String, java.lang.String, java.lang.String, java.io.File[])
	 */
	public void sendEmail(String email, String subject, String text, File attachment) {
	    
	    try {
            
            logger.trace("#sendEmail() - form=[{}], email=[{}], subject=[{}], attachmentName=[{}]", from, email, subject, attachment.getName());
            
            String [] to = new String [] {email};
            
            this.send(to, from, subject, text, attachment);
            
        } catch (Exception e) {
            logger.error("##sendEmail() - An error occurs when sending email.", e);
        }
	    
	}
	
    
    /**
     * sending email helper method
     * 
     * @param to
     * @param from
     * @param subject
     * @param text
     */
    private void send(String[] to, String from, String subject, String text) {
        
        MimeMessage message = javaMailSender.createMimeMessage();
        
        try {
            
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
            
            helper.setTo(to);
            helper.setFrom(new InternetAddress (from, "旭辉学院", "UTF-8"));
            helper.setSubject(subject);
            helper.setText(text, true);
            
            javaMailSender.send(message);
            
        } catch (Exception e) {
            logger.error("##send() - Java send mail occur exception.", e);
            throw BusinessException.newInstance("500", "Java Mail Sender - send mail occur exception");
        } 
        
    }
    
    /**
     * @param to
     * @param from
     * @param subject
     * @param text
     * @param attachment
     */
    private void send(String[] to, String from, String subject, String text, File attachment) {
        
        MimeMessage message = javaMailSender.createMimeMessage();
        
        try {
            
            MimeMessageHelper helper = new MimeMessageHelper(message, true, "utf-8");
      
            helper.setTo(to);
            helper.setFrom(from);
            helper.setSubject(subject);
            helper.setText(text, true);
            
            helper.addAttachment(attachment.getName(), attachment);
            
            javaMailSender.send(message);
            
        } catch (Exception e) {
            logger.error("##send() - Java send mail occur exception.", e);
            throw BusinessException.newInstance("500", "Java Mail Sender - send mail occur exception");
        } 
        
    }

}
